<template>
  <div class="employee-container">
    <div class="header">
      <div class="left">
        <h2>在岗人员管理</h2>
      </div>
      <div class="right">
        <el-input
          v-model="searchQuery"
          placeholder="搜索学生姓名/岗位名称"
          class="search-input"
          clearable
          @clear="handleSearch"
          @keyup.enter="handleSearch"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
      </div>
    </div>

    <el-card class="table-card">
      <el-table
        v-loading="loading"
        :data="employees"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="student.name" label="学生姓名" width="120" />
        <el-table-column prop="position.title" label="岗位名称" min-width="150" />
        <el-table-column prop="position.teacher.name" label="指导教师" width="120" />
        <el-table-column prop="startDate" label="入职时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.startDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="totalWorkHours" label="累计工时" width="100">
          <template #default="{ row }">
            {{ row.totalWorkHours || 0 }}小时
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button-group>
              <el-button
                type="primary"
                size="small"
                @click="handleView(row)"
              >
                查看
              </el-button>
              <el-button
                type="danger"
                size="small"
                @click="handleDelete(row)"
              >
                删除
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>

      <div v-if="!loading && (!employees || employees.length === 0)" class="empty-data">
        <el-empty description="暂无数据" />
      </div>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 查看详情对话框 -->
    <el-dialog
      title="在岗人员详情"
      v-model="viewDialogVisible"
      width="600px"
    >
      <el-descriptions :column="1" border>
        <el-descriptions-item label="学生姓名">
          {{ selectedEmployee?.student.name }}
        </el-descriptions-item>
        <el-descriptions-item label="学生学号">
          {{ selectedEmployee?.student.studentId }}
        </el-descriptions-item>
        <el-descriptions-item label="岗位名称">
          {{ selectedEmployee?.position.title }}
        </el-descriptions-item>
        <el-descriptions-item label="指导教师">
          {{ selectedEmployee?.position.teacher.name }}
        </el-descriptions-item>
        <el-descriptions-item label="工作地点">
          {{ selectedEmployee?.position.workLocation }}
        </el-descriptions-item>
        <el-descriptions-item label="工作时间">
          {{ selectedEmployee?.position.workingHours }}
        </el-descriptions-item>
        <el-descriptions-item label="薪资标准">
          ¥{{ selectedEmployee?.position.salary }}/小时
        </el-descriptions-item>
        <el-descriptions-item label="入职时间">
          {{ formatDate(selectedEmployee?.startDate) }}
        </el-descriptions-item>
        <el-descriptions-item label="累计工时">
          {{ selectedEmployee?.totalWorkHours || 0 }}小时
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getEmployees, deleteEmployee } from '@/api/admin/employee'
import { formatDate } from '@/utils/format'

// 表格数据
const loading = ref(false)
const employees = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const searchQuery = ref('')

// 对话框数据
const viewDialogVisible = ref(false)
const selectedEmployee = ref(null)

// 获取在岗人员列表
const fetchEmployees = async () => {
  try {
    loading.value = true
    const response = await getEmployees({
      page: currentPage.value - 1,
      size: pageSize.value,
      search: searchQuery.value
    })
    
    if (response) {
      employees.value = response.content
      total.value = response.totalElements
    }
  } catch (error) {
    console.error('Failed to fetch employees:', error)
    ElMessage.error('获取在岗人员列表失败')
  } finally {
    loading.value = false
  }
}

// 处理搜索
const handleSearch = () => {
  currentPage.value = 1
  fetchEmployees()
}

// 处理分页
const handleSizeChange = (val) => {
  pageSize.value = val
  fetchEmployees()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchEmployees()
}

// 查看详情
const handleView = (row) => {
  selectedEmployee.value = row
  viewDialogVisible.value = true
}

// 处理删除
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该在岗人员记录吗？', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await deleteEmployee(row.id)
      ElMessage.success('删除成功')
      fetchEmployees()
    } catch (error) {
      console.error('Delete failed:', error)
      ElMessage.error('删除失败')
    }
  })
}

// 在组件挂载时获取数据
onMounted(() => {
  fetchEmployees()
})
</script>

<style scoped>
.employee-container {
  padding: 20px;
}

.header {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.right {
  display: flex;
  gap: 16px;
}

.search-input {
  width: 300px;
}

.table-card {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.empty-data {
  padding: 40px 0;
  text-align: center;
}

:deep(.el-button-group) {
  .el-button {
    padding: 5px 12px;
  }
}
</style> 